package com.cls.common.utils;

import cn.hutool.core.io.FileUtil;
import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.*;

/**
 * fastdfs使用工具类
 *
 * @author Administrator
 */
public class FastDFSUtil {
    private static TrackerServer trackerServer = null;
    private static StorageServer storageServer = null;
    private final static String CONFIG_NAME = "fdfs_client.conf";
    private final static String GROUP_NAME = "group1";

    public static StorageClient getStorageClient() throws IOException, MyException {
        //1.加载配置文件，默认去classpath下加载
        ClientGlobal.init(CONFIG_NAME);
        //2.创建TrackerClient对象
        TrackerClient trackerClient = new TrackerClient();
        //3.创建TrackerServer对象
        trackerServer = trackerClient.getTrackerServer();
        //4.创建StorageServler对象
        storageServer = trackerClient.getStoreStorage(trackerServer);
        //5.创建StorageClient对象，这个对象完成对文件的操作
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        return storageClient;
    }


    /**
     * 文件上传
     *
     * @param fileName 文件名称 需要携带"."以获取扩展名
     * @param data     二进制流
     */
    public static String fileUpload(String fileName, byte[] data) throws IOException, MyException {
        //1. 获取StorageClient对象
        StorageClient storageClient = getStorageClient();
        //2.上传文件  第一个参数：本地文件路径 第二个参数：上传文件的后缀 第三个参数：文件信息
        String[] uploadArray = storageClient.upload_file(data, FileUtil.extName(fileName), null);
        System.out.println(uploadArray[0]);
        return uploadArray[1];
    }

    public static void main(String[] args) throws IOException, MyException {
        File file = new File("E:\\Chrysanthemum.jpg");

        byte[] bytes = new byte[0];
        try {
            bytes = fileToByteArray(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String s = fileUpload("123.jpg", bytes);
        System.out.println(s);
//        boolean b = fileDelete("M00/00/00/wKgB01_NnoSAbcUBAA1rIuRd3Es903.jpg");
//        System.out.println(b);


    }

    /**
     * 下载文件的方法
     *
     * @param fileName 文件名称
     * @return
     */
    public static byte[] fileDetail(String fileName) throws IOException, MyException {
        byte[] bytes = null;

        //1. 获取StorageClient对象
        StorageClient storageClient = getStorageClient();
        //2.下载文件 返回0表示成功，其它均表示失败
        bytes = storageClient.download_file(GROUP_NAME, fileName);

        return bytes;
    }


    /**
     * 删除文件的方法
     * 1表示上传失败  0表示成功
     *
     * @param filePath
     * @return
     */
    public static boolean fileDelete(String filePath) throws IOException, MyException {
        //1. 获取StorageClient对象
        StorageClient storageClient = getStorageClient();
        //2.删除文件 返回0表示成功，其它均表示失败
        int num = storageClient.delete_file(GROUP_NAME, filePath);
        System.out.println(num);
        if (num == 0) {
            return true;
        }
        return false;
    }

    public static byte[] fileToByteArray(File file) throws IOException {
        File f = file;
        if (!f.exists()) {
            throw new FileNotFoundException("file not exists");
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bos.close();
        }
    }

}
